<?php

class ProviderTemplate{
	public static function generateProviderMetabox($post){
	// Add an nonce field so we can check for it later.
	wp_nonce_field( 'provider_meta_box', 'provider_meta_box_nonce' );

	/*
	 * Use get_post_meta() to retrieve an existing value
	 * from the database and use the value for the form.
	 */
	
	$provider_ruc = get_post_meta( $post->ID, '_provider_ruc', true);
	$provider_contact = get_post_meta ( $post->ID, '_provider_contact', true);
	$provider_phone = get_post_meta ( $post->ID, '_provider_phone', true);
	$provider_email = get_post_meta ( $post->ID, '_provider_email', true);
	?>
	
	<p>
	    <label for="meta-textarea" class="prfx-row-title">RUC</label>
	    <input type="text" name="provider_ruc" id="meta-textarea" value="<?php echo esc_attr( $provider_ruc );?>" size="60" />
	</p>
	<p>
	    <label for="meta-textarea" class="prfx-row-title">Contacto</label>
	    <input type="text" name="provider_contact" id="meta-textarea" value="<?php echo esc_attr( $provider_contact );?>" size="60" />
	</p>
	<p>
	    <label for="meta-textarea" class="prfx-row-title">Tel&eacute;fono</label>
	    <input type="text" name="provider_phone" id="meta-textarea" value="<?php echo esc_attr( $provider_phone );?>" size="60" />
	</p>
	<p>
	    <label for="meta-textarea" class="prfx-row-title">Email</label>
	    <input type="text" name="provider_email" id="meta-textarea" value="<?php echo esc_attr( $provider_email );?>" size="60" />
	</p>
	
	<?php
	}

	public static function saveProviderMetabox($post_id){
		

		if ( ! isset( $_POST['provider_meta_box_nonce'] ) ) {
			return;
		}
		if ( ! wp_verify_nonce( $_POST['provider_meta_box_nonce'], 'provider_meta_box' ) ) {
			return;
		}

		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		//TODO: Should check capabilities
		/*if ( isset( $_POST['slide'] ) && 'page' == $_POST['post_type'] ) {
			if ( ! current_user_can( 'edit_slide', $post_id ) ) {
				return;
			}
		} */
		if ( ! isset( $_POST['provider_ruc'] ) ) {
			return;
		}

		
		$provider_ruc = sanitize_text_field( $_POST['provider_ruc'] );
		$provider_contact = sanitize_text_field( $_POST['provider_contact'] );
		$provider_phone = sanitize_text_field( $_POST['provider_phone'] );
		$provider_email = sanitize_text_field( $_POST['provider_email'] );
		update_post_meta( $post_id, '_provider_name', $provider_name );
		update_post_meta( $post_id, '_provider_ruc', $provider_ruc);
		update_post_meta( $post_id, '_provider_contact', $provider_contact );
		update_post_meta( $post_id, '_provider_phone', $provider_phone );
		update_post_meta( $post_id, '_provider_email', $provider_email );
		
	}
}
